<?php
/**
 * 站点库存调整（国内）
 * (model)
 */

namespace App\Models\Inventory;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class AdjustArea extends Model
{
    protected $table = 'inventory_adjust_area';

    protected $guarded = [];

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public static function getDownloadData($re){
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $temp['area'] = $row->area->area ?? '';
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
            $temp['classify'] = $row->sku->classify->title ?? '';
            $temp['b_num'] = $row->b_num;
            $temp['a_num'] = $row->a_num;
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            if(!array_key_exists($row->auser_id,$user)){
                $user[$row->auser_id] = User::getUsernameById($row->auser_id);
            }
            $temp['auser_id'] = $user[$row->auser_id];
            $temp['created_at'] = $row->created_at;
            yield $temp;
        }
    }
}
